package com.example.common.config.seata;

import com.alibaba.cloud.seata.web.SeataHandlerInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import io.seata.rm.datasource.DataSourceProxy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * <p>
 *      使用 seata 数据源来包装原始的数据源
 * </p>
 *
 * @author zyred
 * @since v 0.1
 **/
@Slf4j
@Configuration
public class GuLiMallSeataConfig {

    @Bean
    public DataSource dataSource (DataSourceProperties properties) {
        HikariDataSource source = properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        if (StringUtils.hasText(properties.getName())) {
            source.setPoolName(properties.getName());
        }
        log.info(">>>>>>>>>>>>>>>> common seata init start <<<<<<<<<<<<<<<< ");
        return new DataSourceProxy(source);
    }

    @Bean
    public SeataHandlerInterceptor seataHandlerInterceptor () {
        return new SeataHandlerInterceptor();
    }

}
